package cn.aloxc.tianya.reader;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ActivityMain extends Activity {
	public static final int menu_item_test0 = Menu.FIRST;
	public static final int menu_item_test1 = Menu.FIRST + 1;
	private void hideTitle(){
		//隐藏标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	}
	private void hideStatusBar(){
		//隐藏标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//定义全屏幕参数
		int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
		Window window = this.getWindow();
		window.setFlags(flag, flag);
	}
	OnClickListener listener = null;
	Button button;
	TextView tv;
	public final String RSS_URL = "http://www.tianya.cn/info.txt";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		this.hideTitle();
		this.hideStatusBar();
        setContentView(R.layout.loaddata);
    	setContentView(R.layout.main);	
    	tv = (TextView)findViewById(R.id.mainTxtV);
        listener = new OnClickListener(){
			public void onClick(View v) {
				testit();
			}
        };
        button = (Button)findViewById(R.id.dotest);
        button.setOnClickListener(listener); 
        ImageView image = new ImageView(this);
        Toast toast = new Toast(this);
        image.setImageResource(R.drawable.p52);
        toast.setView(image);
        toast.show();
    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, menu_item_test0, 0, "测试");
		menu.add(0, menu_item_test1, 0, "退出");
		return super.onCreateOptionsMenu(menu);
	}
	public void testit(){
		Intent intent =  new Intent(ActivityMain.this,ActivityTest.class);
		Bundle data = new Bundle();
		String text = (String)((Button)findViewById(R.id.dotest)).getText();				
		data.putString("testItem", text);
		intent.putExtras(data);	
		startActivity(intent);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case menu_item_test0:
				testit();
				break;
			default:
				finish();
				break;
		}
		return super.onOptionsItemSelected(item);
	}
    @SuppressWarnings("unused")
	private boolean getText(){
    	try {
			URL url=new URL(RSS_URL);
			URLConnection conn = url.openConnection();
			InputStream is = conn.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(is);
			ByteArrayBuffer baf = new ByteArrayBuffer(50);
			int current = 0;
			while((current = bis.read()) != -1){
				baf.append((byte)current);
			}
			String ret = new String(baf.toByteArray());
			Log.i("google", ret);
			tv.setText(ret);
			return true;
		} catch (MalformedURLException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
    	
    }
}